package com.huamoxi.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huamoxi.annotation.LogRequest;
import com.huamoxi.consts.ResponseConst;
import com.huamoxi.entity.User;
import com.huamoxi.result.Result;
import com.huamoxi.service.UserService;
import com.huamoxi.utils.ResultUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@LogRequest
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     * @param user 注册用户信息对象
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        if (StrUtil.isBlank(user.getAccount()) || StrUtil.isBlank(user.getPassword()) || user.getSystemId() == null) {
            return ResultUtil.fail("注册失败, 参数错误");
        }

        return userService.register(user);
    }

    /**
     * 根据id查询用户信息
     * @param systemId 系统id
     * @param userId 用户id
     */
    @GetMapping("/{userId}")
    public Result getUserById(@RequestParam("systemId") Long systemId, @PathVariable("userId") Long userId) {
        if (systemId == null) {
            return ResultUtil.fail("查询系统id不能为空");
        }
        if (userId == null) {
            return ResultUtil.fail("查询用户id不能为空");
        }

        User user = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getId, userId).eq(User::getSystemId, systemId));
        if (user != null) {
            user.setPassword(null);
        }

        return ResultUtil.success(user);
    }

    /**
     * 查询用户详情
     * 通过携带的 token 解析后去查询
     */
    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestHeader("Authorization") String authorization) {
        return userService.getUserInfo(authorization.replace("Bearer ", ""));
    }

    // TODO 分页查询
    @GetMapping("/page/{current}/{pageSize}")
    public Result pageUserList(User user, @PathVariable("current") Integer current, @PathVariable("pageSize") Integer pageSize) {
        if (current == null || pageSize == null) {
            return ResultUtil.fail(ResponseConst.ERR_PARAMETER);
        }
        return null;
    }
}
